package com.exsun.ex1077.ui.activity.main

import android.os.Bundle
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.MarkerOptions
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.ActivityFindVehicleBinding
import com.exsun.ex1077.network.entity.MapVehicle
import com.zhangping.amap.AMapHelper
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.view.toView
import com.zhangping.fastjetpack.utils.showMapAppBottomSheet
import kotlinx.android.synthetic.main.activity_find_vehicle.*
import kotlinx.android.synthetic.main.map_cluster_selected.view.*

/**
 * 描述：一键寻车
 * 作者：ZhangPing
 * 创建时间：2023/3/21 14:05
 */
class FindVehicleActivity: BaseActivity<BaseViewModel, ActivityFindVehicleBinding>() {

    override fun title() = "一键寻车"

    private lateinit var model: MapVehicle
    private lateinit var mapHelper: AMapHelper

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        model = intent.getSerializableExtra("model") as MapVehicle
        address.text = model.location
        mapHelper = AMapHelper(mapView.map).apply {
            bindLifecycle(lifecycle, mapView)
            hideLogo()
            applyStyle()
            applyCustomStyle(R.raw.style, R.raw.style_extra)
            moveMap(model.position)
        }
        //添加车辆图标
        val view = R.layout.map_cluster_selected.toView()
        view.iv_arrow.setImageResource(if (model.isOnline > 0) R.drawable.cluster_direction_selected else R.drawable.cluster_direction_offline)
        view.iv_bg.setImageResource(if (model.isOnline > 0) R.drawable.cluster_online_selected else R.drawable.cluster_offline)
        view.iv_arrow.rotation = model.dre.toFloat()
        mapHelper.map.addMarker(MarkerOptions()
            .anchor(0.5F, 0.5F)
            .position(model.position)
            .icon(BitmapDescriptorFactory.fromView(view)))
        //打开第三方APP
        openMapApp.setOnClickListener {
            val endLat = model.lat
            val endLng = model.lng
            val endName = model.location
            showMapAppBottomSheet(this, endLat, endLng, endName)
        }
    }

}